<script setup lang="ts">
import { useConfigStore } from "./stores/config.store";
import { onMounted } from "vue";
import { BaseApi } from "./apis/base.api";
import { useWebxDict } from "@webxue/vue3-antdv-ui";
import { UtilTool } from "@webxue/common";
import { useUserStore } from "@/stores/user.store";
import zhCN from "ant-design-vue/es/locale/zh_CN";
import "dayjs/locale/zh-cn";

// 字典初始化
const { loading, init } = useWebxDict();

onMounted(async () => {
  // 设置日期语言
  UtilTool.dayjs().locale("zh-cn");
  // 设置系统主题色
  useConfigStore().setProviderTheme();
  // 设置免鉴权配置
  await useConfigStore().setConfigJson();
  // 字典初始化
  await init({
    api:{
      // 获取更新的字典类型列表
      async getUpdateDictTypeList(updateDate:string){
        const dictTypeRes = await BaseApi.dictType.listByUpdate({ updateDate }, { requestType:"noToken" });
        return Promise.resolve(dictTypeRes.data);
      },
      // 获取更新的字典列表
      async getUpdateDictDataList(dictTypeList:any[]){
        // 查询每个更新的字典类型的字典值
        return (await Promise.all(dictTypeList.map(async (dictTypeData:any) => {
          const dictDataRes = await BaseApi.dictData.list({ queryType:"all", dictType:dictTypeData.dictType }, { requestType:"noToken" });
          return { dictType:dictTypeData.dictType, dictList:dictDataRes.data };
        }))).reduce((result:Record<string, any>,current:{ dictType:string, dictList:any[] }) => {
          result[current.dictType] = current.dictList;
          return result;
        },{});
      }
    }
  })
});
</script>

<template>
  <div>
    <!-- 字典初始化 -->
    <WebxDictLoading :loading="loading" />

    <AConfigProvider :theme="useConfigStore().providerTheme" :locale="zhCN">
      <WebxLayoutRoot>
        <RouterView />
      </WebxLayoutRoot>
    </AConfigProvider>
  </div>
</template>

<style lang="less" scoped></style>
